package com.neusoft.neuiotms.dcom.controller.change;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.file.dto.FileDTO;
import com.neusoft.bizcore.file.service.FileService;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.neuiotms.dcom.dto.change.ChangeImplementationDTO;
import com.neusoft.neuiotms.dcom.dto.change.ChangeRegistrationDTO;
import com.neusoft.neuiotms.dcom.service.change.ChangeImplementationService;
import com.neusoft.neuiotms.dcom.service.change.ChangeRegistrationService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "变更实施")
@RestController
@RequestMapping(value = "/api/w/change/implementation")
public class ChangeImplementationController {

    @Autowired
    private ChangeImplementationService changeImplementationService;

    @Autowired
    private FileService fileService;

    @Autowired
    private ChangeRegistrationService changeRegistrationService;

    @ApiOperation("新增变更实施工单")
    @PostMapping
    public ResultDTO<ChangeImplementationDTO> create(@RequestBody final ChangeImplementationDTO dto,
            final UserBean user) {
        return ResultDTO.success(this.changeImplementationService.create(dto, user));
    }

    @ApiOperation("查看变更实施工单")
    @GetMapping("/{workOrderNum}/{taskId}")
    public ResultDTO<ChangeImplementationDTO> details(@PathVariable final String workOrderNum,
            @PathVariable final String taskId,
            final UserBean user) {
        return ResultDTO.success(this.changeImplementationService.detail(workOrderNum, taskId, user));
    }

    @ApiOperation("上传文件")
    @PostMapping("/upload/attachment")
    public ResultDTO<FileDTO> upload(@RequestParam("file") final MultipartFile file) {
        final FileDTO doc = this.fileService.save(file);
        return ResultDTO.success(doc);
    }

    @ApiOperation("下载文件")
    @GetMapping("/download/{fsId}")
    public void download(@PathVariable final String fsId, final HttpServletResponse response) {
        this.fileService.download(fsId, response);
    }

    @ApiOperation("删除文件")
    @DeleteMapping("/delete/attachment/{fsId}")
    public void deleteAttachment(@PathVariable final String fsId) {
        this.changeImplementationService.deleteMongoFile(fsId);
    }

    @ApiOperation("获取配置项ID")
    @GetMapping("/ciId/{workOrderNum}")
    public ResultDTO<Object> getCiId(@PathVariable final String workOrderNum) {
        final ChangeRegistrationDTO changeRegistrationDTO = this.changeRegistrationService.findByWorkNum(workOrderNum);
        return ResultDTO.success(changeRegistrationDTO.getCiId());
    }
}
